
package com.benimetum.managers;

import com.benimetum.database.Sparql;
import com.benimetum.exceptions.DatabaseConnectException;
import com.benimetum.exceptions.DatabaseWriteOrDeleteException;
import com.benimetum.items.Student;
import com.benimetum.items.Teacher;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class StudentManager
{
    private final static String RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    private final static String ETU="http://www.benimetum.com/benimetum.owl#";
    private final static String FOAF="http://xmlns.com/foaf/0.1/";
    
    /**
     * creates student
     * @param email
     * @param password
     * @param id
     * @param firstName
     * @param lastName
     * @param profilePicture
     * @param teacherId 
     */
    public static boolean createStudent(String email, String password, String id,String firstName, String lastName, String profilePicture, String teacherId){
	  try {
		Sparql.insertProperty(ETU+"st"+id, RDF+"type", ETU+"Student");
		Sparql.insertProperty(ETU+"st"+id, ETU+"hasMentor", ETU+teacherId);
		Sparql.insertProperty(ETU+teacherId, ETU+"mentored", ETU+"st"+id);
		Sparql.insertLiteral(ETU+"st"+id, FOAF+"firstName", firstName);
		Sparql.insertLiteral(ETU+"st"+id, FOAF+"surname", lastName);
		Sparql.insertLiteral(ETU+"st"+id, ETU+"id", "st"+id);
		Sparql.insertLiteral(ETU+"st"+id, ETU+"email", email);
		Sparql.insertLiteral(ETU+"st"+id, ETU+"status", "null");
		Sparql.insertLiteral(ETU+"st"+id, ETU+"password", password);
		Sparql.insertLiteral(ETU+"st"+id, ETU+"profilePicture", profilePicture);
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * dersi öğrenci üzerine ekler
     * @param courseId
     * @param studentId 
     */
    public static void enrollStudent(String courseId, String studentId) {
	  try {
		Sparql.insertProperty(ETU+courseId, ETU+"hasStudents", ETU+studentId);
		Sparql.insertProperty(ETU+studentId, ETU+"hasCourse", ETU+courseId);
	  } catch (DatabaseWriteOrDeleteException ex) {
		Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    /**
     * dersi öğrenci üzerinden siler
     * @param courseId
     * @param studentId 
     */
    public static void withdrawStudent(String courseId, String studentId) {
	  try {
		Sparql.removeResource(ETU+courseId, ETU+"hasStudents", ETU+studentId);
		Sparql.removeResource(ETU+studentId, ETU+"hasCourse", ETU+courseId);
	  } catch (DatabaseConnectException ex) {
		Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
	  } catch (DatabaseWriteOrDeleteException ex) {
		Logger.getLogger(StudentManager.class.getName()).log(Level.SEVERE, null, ex);
	  }
    }
    
    /**
     * öğrenci bilgilerini id bilgisi ile bulur
     * @param id
     * @return 
     */
    public static Student findStudentById(String id){
	  String query="select ?email ?firstName ?surname ?picture ?status ?teacherId "
		    + "where {?student rdf:type etu:Student. ?student etu:id \""+id+"\"."
		    + "?student etu:email ?email. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?surname."
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Teacher teacher=TeacherManager.getTeacherById(toList.get(0).get("teacherId").toString());
	  Student student=new Student(toList.get(0).get("email").toString(), id, toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(),toList.get(0).get("picture").toString(), teacher);
	  return student;
    }
    
    public static boolean updateStudentsStatus(String id, String status){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"status");
		Sparql.insertLiteral(ETU+id, ETU+"status",status);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static String getStudentStatus(String id){
	  String query="select ?status where {?student rdf:type etu:Student. ?student etu:id \""+id+"\". ?student etu:status ?status.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(toList.get(0).get("status").toString().equals("null"))
		return null;
	  return toList.get(0).get("status").toString();
    }
    
    public static Student checkUserNamePassword(String email, String password){
	  String query="select ?id ?firstName ?surname ?picture ?status ?teacherId "
		    + "where {?student rdf:type etu:Student. ?student etu:id ?id. "
		    + "?student etu:email \""+email+"\". ?student etu:password \""+password+"\" .?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?surname."
		    + "?student etu:profilePicture ?picture. ?student etu:hasMentor ?teacher. ?teacher etu:id ?teacherId.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		Teacher teacher=TeacherManager.getTeacherById(toList.get(0).get("teacherId").toString());
		Student student=new Student(email, toList.get(0).get("id").toString(), toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(),toList.get(0).get("picture").toString(), teacher);
		return student;
	  }
	  return null;
    }
    
    public static boolean checkUserName(String email){
	  String query="select ?id "
		    + "where {?student rdf:type etu:Student. ?student etu:id ?id. "
		    + "?student etu:email \""+email+"\".}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		return true;
	  }
	  return false;
    }
    
    public static String getIdByUserName(String email){
	  String query="select ?id "
		    + "where {?student rdf:type etu:Student. ?student etu:id ?id. "
		    + "?student etu:email \""+email+"\".}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		return toList.get(0).get("id").toString();
	  }
	  return null;
    }
    
    public static String getPassword(String id){
	  String query="select ?password "
		    + "where {?student rdf:type etu:Student. ?student etu:id \""+id+"\"."
		    + "?student etu:password ?password.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty())
		return toList.get(0).get("password").toString();
	  return null;
    }
        
    public static boolean isSameStudent(String id1, String id2){
	  return id1==id2;
    }
    
    public static boolean updateEmail(String id, String newEmail){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"email");
		Sparql.insertLiteral(ETU+id, ETU+"email",newEmail);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static boolean updateFirstName(String id, String name){
	  try {
		Sparql.removeLiteral(ETU+id, FOAF+"firstName");
		Sparql.insertLiteral(ETU+id, FOAF+"firstName",name);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static boolean updateLastName(String id, String name){
	  try {
		Sparql.removeLiteral(ETU+id, FOAF+"surname");
		Sparql.insertLiteral(ETU+id, FOAF+"surname",name);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static boolean updatePassword(String id, String password){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"password");
		Sparql.insertLiteral(ETU+id, ETU+"password",password);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    private static boolean updateProfilePicture(String id, String picture){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"profilePicture");
		Sparql.insertLiteral(ETU+id, ETU+"profilePicture",picture);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static boolean changeProfilePicture(File file, String id){
	  return FileManager.addProfilePicture(file, id);
    }
}
